package com.controller;

import com.orm.ServiceCalledEntity;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lirance on 16/5/10.
 */
public class GetMaeRmse {
    public List<Double> GetMaeRmse(double lmd, int k, int clusteNum, int Gn) throws Exception {
        List<Double> results = new ArrayList<>();
        double mae = 0;
        double rmse = 0;
        double rtt, prRtt;
        GetServiceCallList gs = new GetServiceCallList();
        gs.setGn(Gn);

        List<ServiceCalledEntity> l = gs.GetServiceCallLists();
        List<Integer> userlist = new ArrayList<>();  //用户列表
        List<Integer> slist = new ArrayList<>();

        for (int i = 0; i < l.size(); i++) {  //获取调用了服务的用户
            if (!userlist.contains(l.get(i).getuId())) {
                userlist.add(l.get(i).getuId());
            }

            if (!slist.contains(l.get(i).getWsId())) {
                slist.add(l.get(i).getWsId());       //获取所有服务列表
            }

        }

        int unum = userlist.size();
        if (unum > 20) unum = 20;
        int num = 0;

        if (clusteNum == 0) {//isClusted=faluse,非聚类方法
            for (int i = 0; i < unum; i++) {
                int uid = userlist.get(i);  //用户id
                List<ServiceCalledEntity> l1 = gs.GetServiceCallListsByuId(uid); //用户uid的服务列表
                List<Integer> servicelist = new ArrayList<>(); //用户的服务列表
                List<Integer> sclist = slist;

                for (int j = 0; j < l1.size(); j++) {
                    if (!servicelist.contains(l1.get(j).getWsId())) {
                        servicelist.add(l1.get(j).getWsId());
                    }
                }

                sclist.removeAll(servicelist);  //差集
                int snum = sclist.size();
                if (snum > 20) snum = 20;

                for (int j = 0; j < snum; j++) {
                    rtt = getAverageRtt(uid, sclist.get(j)).getAveRtt();  //获取rtt
                    prRtt = new Predict().getPredicts(uid, sclist.get(j), lmd, k, Gn);
                    mae = mae + Math.abs(prRtt - rtt);
                    rmse = rmse + (prRtt - rtt) * (prRtt - rtt);
                    num++;

                }
            }

            mae = mae / num;
            rmse = Math.sqrt(rmse / num);
        } else {//聚类方法
            new ClusterAnalysis().insert(clusteNum);   //聚类
            for (int i = 0; i < unum; i++) {
                int uid = userlist.get(i);  //用户id
                List<ServiceCalledEntity> l1 = gs.GetServiceCallListsByuId(uid); //用户uid的服务列表
                List<Integer> servicelist = new ArrayList<>(); //用户的服务列表
                List<Integer> sclist = slist;  //用户未调用的服务列表

                for (int j = 0; j < l1.size(); j++) {
                    if (!servicelist.contains(l1.get(j).getWsId())) {
                        servicelist.add(l1.get(j).getWsId());
                    }
                }

                sclist.removeAll(servicelist);  //差集
                int snum = sclist.size();
                if (snum > 20) snum = 20;

                for (int j = 0; j < snum; j++) {
                    rtt = getAverageRtt(uid, sclist.get(j)).getAveRtt();  //获取rtt
                    prRtt = new Predict().getPredicts_Cluster(uid, sclist.get(j), lmd, k, Gn);
                    mae = mae + Math.abs(prRtt - rtt);
                    rmse = rmse + (prRtt - rtt) * (prRtt - rtt);
                    num++;
                }
            }
            mae = mae / num;
            rmse = Math.sqrt(rmse / num);
        }
        results.add(mae);
        results.add(rmse);
        return results;
    }

   /* public double GetRmse(double lmd, int k, boolean isClusted) throws Exception {
        // double rmse = 0;
        double rtt, prRtt;
        List userlist = new GetUsersList().GetUserLists();
        List servicelist = new GetServiceList().GetServiceLists();
        List<SavedSCRtt> rtts = this.getAverageRtt();

        int unum = userlist.size();
        int snum = servicelist.size();
        int num = 0;

        if (!isClusted) {//isClusted=faluse,非聚类方法
            for (int i = 1; i < unum; i++) {
                for (int j = 1; j < snum; j++) {
                    num++;
                    rtt = rtts.get(num).getAveRtt();
                    prRtt = new Predict().getPredicts(i, j, lmd, k);
                    rmse = rmse + Math.abs(prRtt - rtt);
                }
            }
            rmse = Math.sqrt(rmse);
            rmse = rmse / num;
        } else {//聚类方法
            for (int i = 1; i < unum; i++) {
                for (int j = 1; j < snum; j++) {
                    num++;
                    rtt = rtts.get(num).getAveRtt();
                    prRtt = new Predict().getPredicts_Cluster(i, j, lmd, k);
                    rmse = rmse + Math.abs(prRtt - rtt) * Math.abs(prRtt - rtt);
                }
            }
            rmse = rmse / num;
        }
        return rmse;
    }*/

    private SavedSCRtt getAverageRtt(int uid, int sid) {

        SavedSCRtt rtt = new SavedSCRtt();

        String path = "/Users/lirance/Documents/ServiceRecommendationSystem/src/com/controller/averageRtt.txt";

        try {
            File file = new File(path);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            while (br.ready()) {
                line = br.readLine();
                String[] data = line.split("\t");
                if (data[0].equals(uid + "") && data[1].equals(sid + "")) {
                    rtt.setUid(Integer.parseInt(data[0]));
                    rtt.setWsid(Integer.parseInt(data[1]));
                    rtt.setAveRtt(Double.parseDouble(data[2]));
                    break;
                }
            }
            br.close();
            fr.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rtt;  //返回的averageRtt 列表序号为id-1
    }

}
